package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

import Dao.RequestTransferDao;
import Entity.Transfer;
import Model.RequestTransferModel;
import Model.SearchModel;
import View.RequestTransferView;
import View.SearchView;

public class RequestTransferController {
	private RequestTransferView view;
	private RequestTransferModel model;
	RequestTransferDao dao ;
	private JInternalFrame frame;
	
	public RequestTransferController(){
		frame = new JInternalFrame();
		view = new RequestTransferView();
		model = new RequestTransferModel();
		view.setModel(model);
		doAction();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(view);
		frame.setVisible(true);
		frame.setSize(800, 600);
	}

	private void doAction(){
		// TODO Auto-generated method stub
		doTransfer();
		loadTable();
		doSelectTable();
		doExit();
		doApproval();
		doRefuse();
	}

	private void doTransfer() {
		// TODO Auto-generated method stub
		try {
			model.transfer();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doRefuse() {
		// TODO Auto-generated method stub
		view.doRefuse(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dao = new RequestTransferDao();
				try {
					int row = view.getSelectedItem();
					int ID = Integer.parseInt(view.getValueAt(row, 0).toString());
					dao.Refuse(ID);
					JOptionPane.showMessageDialog(null, "You had Refuse a Request transfer!");
					view.clearAll();
					model.transfer();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}

	private void doApproval() {
		// TODO Auto-generated method stub
		view.doApproval(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				dao = new RequestTransferDao();
				try {
					int row = view.getSelectedItem();
					int ID = Integer.parseInt(view.getValueAt(row, 0).toString());
					dao.Approval(ID);
					JOptionPane.showMessageDialog(null, "You had Approval a Request transfer!");
					view.clearAll();
					model.transfer();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}

	private void doSelectTable() {
		// TODO Auto-generated method stub
		view.doSelectTable(new KeyAdapter() {

			public void keyReleased(KeyEvent e) {
				doSelect();
			}
		});

		view.doSelectTable(new MouseAdapter() {

			public void mouseClicked(MouseEvent e) {
				doSelect();
			}
		});
	}
	private void doSelect(){
		int row = view.getSelectedItem();
		dao = new RequestTransferDao();
//		Transfer transfer = new Transfer(Integer.parseInt(view.getValueAt(row, 0).toString()), view.getValueAt(row, 1).toString(), view.getValueAt(row, 2).toString(), view.getValueAt(row, 3).toString(), view.getValueAt(row, 4).toString(), view.getValueAt(row, 5).toString());
		Transfer transfer;
		try {
			transfer = dao.selectTableLoad(Integer.parseInt(view.getValueAt(row, 0).toString()));
			view.setName(transfer.getEmpName());
			view.setNoTranfer(""+transfer.getID());
			view.setNumber(transfer.getEmpNumber());
			view.setToLocation(transfer.getToLocation());
			view.setFromLocation(transfer.getFromLocation());
			view.setToProject(transfer.getToProject());
			view.setFromProject(transfer.getFromProject());
			view.setToDept(transfer.getToDepartment());
			view.setFromDept(transfer.getFromDepartment());
			view.enable();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void doExit() {
		// TODO Auto-generated method stub
		view.doExit(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				frame.dispose();
			}
		});
	}

	private void loadTable(){
		// TODO Auto-generated method stub
		try {
			model.transfer();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		view.setModel(model);
	}
	public static void main(String[] agrs) throws IOException, ClassNotFoundException, SQLException {
		new RequestTransferController();
	}

	public JInternalFrame getFrame() {
		// TODO Auto-generated method stub
		return this.frame;
	}
}
